home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / DIAGTOOL / MEMSZ240.ZIP;1 / DEBUG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-28  |  7.9 KB  |  216 lines

  1. /****************************************************************** DEBUG.CPP
  2.  *                                                                          *
  3.  *  Debugging Aids                                                          *
  4.  *                                                                          *
  5.  ****************************************************************************/
  6.  
  7. #define INCL_BASE
  8. #define INCL_PM
  9. #include <os2.h>
  10.  
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16.  
  17. #include "debug.h"
  18. #include "mutex.h"
  19.  
  20. extern HFILE Timer = 0 ;
  21. extern BOOL Trace = FALSE ;
  22.  
  23.  
  24. /****************************************************************************
  25.  *                                                                          *
  26.  *                       Display Debug Message                              *
  27.  *                                                                          *
  28.  ****************************************************************************/
  29.  
  30. extern VOID Debug ( HWND hwnd, char *Message, ... )
  31. {
  32.  /***************************************************************************
  33.   * Local Declarations                                                      *
  34.   ***************************************************************************/
  35.  
  36.   va_list Marker ;
  37.   char Text [500] ;
  38.  
  39.  /***************************************************************************
  40.   * Format the debug message.                                               *
  41.   ***************************************************************************/
  42.  
  43.   va_start ( Marker, Message ) ;
  44.   vsprintf ( Text, Message, Marker ) ;
  45.   va_end ( Marker ) ;
  46.  
  47.  /***************************************************************************
  48.   * Display the log message and wait for the user to press ENTER.           *
  49.   ***************************************************************************/
  50.  
  51.   WinMessageBox ( HWND_DESKTOP, hwnd, PSZ(Text), PSZ("Debug"), 0, MB_ENTER ) ;
  52. }
  53.  
  54. /****************************************************************************
  55.  *                                                                          *
  56.  *                         Log Debug Message                                *
  57.  *                                                                          *
  58.  ****************************************************************************/
  59.  
  60. Mutex LogSemaphore ( PSZ(NULL) ) ;
  61.  
  62. extern VOID Log ( char *Message, ... ) {
  63.  
  64.  /***************************************************************************
  65.   * Serialize this function by requesting its semaphore.                    *
  66.   ***************************************************************************/
  67.  
  68.   LogSemaphore.Request ( SEM_INDEFINITE_WAIT ) ;
  69.  
  70.  /***************************************************************************
  71.   * Open the log file.                                                      *
  72.   ***************************************************************************/
  73.  
  74.   FILE *File = fopen ( "MEMSIZE.LOG", "a" ) ;
  75.  
  76.  /***************************************************************************
  77.   * If the file got opened, write the message to the log file and close it. *
  78.   ***************************************************************************/
  79.  
  80.   if ( File ) {
  81.     char Time [9], Date [9] ;
  82.     fprintf ( File, "%s %s ", _strtime(Time), _strdate(Date) ) ;
  83.     va_list Marker ;
  84.     va_start ( Marker, Message ) ;
  85.     vfprintf ( File, Message, Marker ) ;
  86.     va_end ( Marker ) ;
  87.     fprintf ( File, "\n" ) ;
  88.     fclose ( File ) ;
  89.   } /* endif */
  90.  
  91.  /***************************************************************************
  92.   * Release the function semaphore and return.                              *
  93.   ***************************************************************************/
  94.  
  95.   LogSemaphore.Release ( ) ;
  96. }
  97.  
  98. /****************************************************************************
  99.  *                                                                          *
  100.  *                          Open Timer for Use                              *
  101.  *                                                                          *
  102.  ****************************************************************************/
  103.  
  104. extern BOOL OpenTimer ( VOID )
  105. {
  106.   if ( Timer )
  107.     DosClose ( Timer ) ;
  108.  
  109.   ULONG Action ;
  110.   if ( DosOpen ( (PSZ)"TIMER$", &Timer, &Action, 0, FILE_NORMAL, FILE_OPEN, OPEN_SHARE_DENYNONE, 0 ) )
  111.   {
  112.     return ( FALSE ) ;
  113.   }
  114.  
  115.   return ( TRUE ) ;
  116. }
  117.  
  118. /****************************************************************************
  119.  *                                                                          *
  120.  *                              Close Timer                                 *
  121.  *                                                                          *
  122.  ****************************************************************************/
  123.  
  124. extern VOID CloseTimer ( VOID )
  125. {
  126.   DosClose ( Timer ) ;
  127. }
  128.  
  129. /****************************************************************************
  130.  *                                                                          *
  131.  *                       Read Time from HRTIMER.SYS                         *
  132.  *                                                                          *
  133.  ****************************************************************************/
  134.  
  135. extern BOOL GetTime ( PTIMESTAMP pts )
  136. {
  137.   ULONG ByteCount ;
  138.  
  139.   if ( DosRead ( Timer, pts, sizeof(*pts), &ByteCount ) )
  140.     return ( FALSE ) ;
  141.  
  142.   return ( TRUE ) ;
  143. }
  144.  
  145. /****************************************************************************
  146.  *                                                                          *
  147.  *                         Calculate Elapsed Time                           *
  148.  *                                                                          *
  149.  ****************************************************************************/
  150.  
  151. extern ULONG ComputeElapsedTime ( PTIMESTAMP ptsStart, PTIMESTAMP ptsStop, PULONG pulNs )
  152. {
  153.   ULONG ulMsecs, ulNsecs;
  154.   TIMESTAMP tsStart, tsStop ;
  155.  
  156.   tsStart = *ptsStart ;                       // De-reference timestamp
  157.                                               //     structures for speed
  158.   tsStop  = *ptsStop ;
  159.  
  160.   ulMsecs = tsStop.ulMs - tsStart.ulMs ;      // Elapsed milliseconds
  161.  
  162.   if( tsStart.ulNs > tsStop.ulNs )            // If nanosecond overflow ...
  163.   {
  164.     ulNsecs = (1000000 + tsStop.ulNs) - tsStart.ulNs; // Adjust nanoseconds
  165.     ulMsecs--;                                        // Adjust milliseconds
  166.   }
  167.   else
  168.     ulNsecs = tsStop.ulNs - tsStart.ulNs ;    // No overflow..Elapsed nanos
  169.  
  170.   *pulNs = ulNsecs ;
  171.  
  172.   return ( ulMsecs ) ;
  173. }
  174.  
  175. /****************************************************************************
  176.  *                                                                          *
  177.  *  Allocate Memory                                                         *
  178.  *                                                                          *
  179.  ****************************************************************************/
  180.  
  181. //#define ALLOCATE_THROUGH_DOS
  182.  
  183. extern PVOID AllocateMemory ( ULONG ByteCount )
  184. {
  185.   #ifdef ALLOCATE_THROUGH_DOS
  186.   {
  187.     PVOID Memory ;
  188.     DosAllocMem ( &Memory, ByteCount, PAG_READ | PAG_WRITE | PAG_COMMIT ) ;
  189.     return ( Memory ) ;
  190.   }
  191.   #else
  192.   {
  193.     return ( malloc ( ByteCount ) ) ;
  194.   }
  195.   #endif
  196. }
  197.  
  198. /****************************************************************************
  199.  *                                                                          *
  200.  *  Free Memory                                                             *
  201.  *                                                                          *
  202.  ****************************************************************************/
  203.  
  204. extern VOID FreeMemory ( PVOID Memory )
  205. {
  206.   #ifdef ALLOCATE_THROUGH_DOS
  207.   {
  208.     DosFreeMem ( Memory ) ;
  209.   }
  210.   #else
  211.   {
  212.     free ( Memory ) ;
  213.   }
  214.   #endif
  215. }
  216.